
const strCompareOptions = [
  {name: '为其之一', symbol: 'IN', compare: (a, b) => (b || []).includes(a)},
  {name: '不为其之一', symbol: 'NIN', compare: (a, b) => !(b || []).includes(a)},
]

const strArrCompareOptions = [
  {name: '包含', symbol: 'HS', compare: (a, b) => checkElementsExistInArray(b, a)},
  {name: '不包含', symbol: 'NHS', compare: (a, b) => !checkElementsExistInArray(b, a)},
]

/**
 * 加载比较函数对象
 * @return {{strCompare: {}, strArrCompare: {}}}
 */
function getCompareFucs() {
  let cpFuncs = {
    strCompare: {},
    strArrCompare: {}
  };
  strCompareOptions.forEach(v => (cpFuncs.strCompare[v.symbol] = v.compare));
  strArrCompareOptions.forEach(
      v => (cpFuncs.strArrCompare[v.symbol] = v.compare)
  );
  return cpFuncs;
}

/**
 * 校验数组A是否包含在B内
 * @param arrOne
 * @param arrTwo
 * @param key
 * @return {*|boolean}
 */
export const checkElementsExistInArray = (arrOne, arrTwo, key) => {
  if (!arrOne?.length || !arrTwo?.length) {
    return false;
  }
  const setTwo = new Set(key ? arrTwo.map(item => item[key]) : arrTwo);
  return arrOne.every(item => setTwo.has(key ? item[key] : item));
};


export const CompareFuncs = getCompareFucs()

export default {
  strCompareOptions, strArrCompareOptions
}
